"use strict";
const common_vendor = require("../common/vendor.js");
const baseURL = "http://localhost:8080";
const request = (options) => {
  return new Promise((resolve, reject) => {
    let url = baseURL + options.url;
    if (options.method === "GET" && options.data) {
      const queryString = Object.entries(options.data).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join("&");
      url += `?${queryString}`;
    }
    const cookie = common_vendor.index.getStorageSync("cookie");
    common_vendor.index.request({
      url,
      method: options.method || "GET",
      data: options.method === "POST" ? JSON.stringify(options.data) : void 0,
      header: {
        "Content-Type": "application/json;charset=UTF-8",
        "Cookie": cookie,
        ...options.header
      },
      success: (res) => {
        if (res.header["Set-Cookie"]) {
          common_vendor.index.setStorageSync("cookie", res.header["Set-Cookie"]);
        }
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          common_vendor.index.showToast({
            title: res.data.message || "请求失败",
            icon: "none"
          });
          reject(res.data);
        }
      },
      fail: (err) => {
        console.error("Request Error:", err);
        common_vendor.index.showToast({
          title: "网络错误",
          icon: "none"
        });
        reject(err);
      }
    });
  });
};
exports.request = request;
